package com.kaluli.project.service;
// 文件名 SendEmail.java
 
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.kaluli.project.utils.DateUtil;
@Service
@Configuration
@Component
@PropertySource({ "classpath:mail.properties" })
public class SendEmailService
{
	
	@Autowired
	private Environment env;
	
	public void sendExmail(String content,String emails) throws MessagingException{
		    Properties props = new Properties();  
	        // 开启debug调试  
	        props.setProperty("mail.debug", "false");  
	        // 发送服务器需要身份验证  
	        props.setProperty("mail.smtp.auth", "true");  
	        // 设置邮件服务器主机名  
	        props.setProperty("mail.host", env.getProperty("nsapi.mail.smtp"));  
	        // 发送邮件协议名称  
	        props.setProperty("mail.transport.protocol", "smtp");  
	          
	        // 设置环境信息  
	        Session session = Session.getInstance(props);  
	          
	        // 创建邮件对象  
	        Message msg = new MimeMessage(session);  
	        String time =DateUtil.date2Str(new Date(), "yyyy-MM-dd");
	        msg.setSubject(env.getProperty("nsapi.mail.title")+"&"+time);  
	        // 设置邮件内容  
	        msg.setText(content);
	        
	        //设置自定义发件人昵称    
	        String nick="";    
	        try {    
	            nick=javax.mail.internet.MimeUtility.encodeText(env.getProperty("nsapi.mail.kaluli"));    
	        } catch (UnsupportedEncodingException e) {
	            e.printStackTrace();    
	        }
			
	        // 设置发件人 
//	        msg.setFrom(new InternetAddress(env.getProperty("nsapi.mail.sender")));  
	        msg.setFrom(new InternetAddress(nick+" <"+env.getProperty("nsapi.mail.sender")+">")); 
	        
	        
	        Transport transport = session.getTransport();  
//	        String emails=env.getProperty("nsapi.mail.receiver");
	        String[] s =emails.split(",");
	        InternetAddress[] address = new InternetAddress[s.length];
	        for(int i=0;i<s.length;i++){
	        	address[i]=new InternetAddress(s[i]);
	        }
	        // 连接邮件服务器  (用户名密码)
	        transport.connect(env.getProperty("nsapi.mail.username"), env.getProperty("nsapi.mail.password"));  
	        // 发送邮件  
	        transport.sendMessage(msg, address);
	        // 关闭连接  
	        transport.close();  
	}
	
	
	public void sendExmailHtml(String content,String emails,String title) throws MessagingException{
	    Properties props = new Properties();  
        // 开启debug调试  
        props.setProperty("mail.debug", "false");  
        // 发送服务器需要身份验证  
        props.setProperty("mail.smtp.auth", "true");  
        // 设置邮件服务器主机名  
        props.setProperty("mail.host", env.getProperty("nsapi.mail.smtp"));  
        // 发送邮件协议名称  
        props.setProperty("mail.transport.protocol", "smtp");  
          
        // 设置环境信息  
        Session session = Session.getInstance(props);  
          
        // 创建邮件对象  
        Message msg = new MimeMessage(session);  
        String time =DateUtil.date2Str(new Date(), "yyyy-MM-dd");
        msg.setSubject(title+"&"+time);  
        // 设置邮件内容  
        msg.setText(content);
        
        //设置自定义发件人昵称    
        String nick="";    
        try {
            nick=javax.mail.internet.MimeUtility.encodeText(env.getProperty("nsapi.mail.kaluli"));    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();    
        }
		
        // 设置发件人 
//        msg.setFrom(new InternetAddress(env.getProperty("nsapi.mail.sender")));  
        msg.setFrom(new InternetAddress(nick+" <"+env.getProperty("nsapi.mail.sender")+">")); 
        
        
        
        //MimeMultipart类是一个容器类，包含MimeBodyPart类型的对象  
        Multipart mainPart = new MimeMultipart();  
        MimeBodyPart messageBodyPart = new MimeBodyPart();//创建一个包含HTML内容的MimeBodyPart  
        //设置HTML内容  
        messageBodyPart.setContent(content,"text/html; charset=utf-8");  
        mainPart.addBodyPart(messageBodyPart);
      //将MimeMultipart对象设置为邮件内容     
        msg.setContent(mainPart);
          
        Transport transport = session.getTransport();  
//        String emails=env.getProperty("nsapi.mail.receiver");
        String[] s =emails.split(",");
        InternetAddress[] address = new InternetAddress[s.length];
        for(int i=0;i<s.length;i++){
        	address[i]=new InternetAddress(s[i]);
        }
        // 连接邮件服务器  (用户名密码)
        transport.connect(env.getProperty("nsapi.mail.username"), env.getProperty("nsapi.mail.password"));  
        // 发送邮件  
        transport.sendMessage(msg, address);
        // 关闭连接  
        transport.close();  
}
	
	
}